import { createRouter, createWebHistory } from 'vue-router';
import { staticRoutes } from './routes';
import { useUserStore } from '@/stores/user';

const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes,
  scrollBehavior() {
    return { top: 0, left: 0 }
  },
})
let blackList = ['/ucenter', '/cart']
//路由前置守卫
router.beforeEach((to, from, next) => {
  // console.log(blackList.includes(to.fullPath))
  // console.log('to', to)
  // console.log('from', from)
  //判断将要跳转的路由是不是在黑名单中,如果不在直接跳转,如果在,判断有没有登录,既判断有没有token
  if (!blackList.includes(to.fullPath)) {
    // console.log('放行')
    next()
  } else {
    // console.log('去登录页')
    //获取token
    const userStore = useUserStore()
    let token = userStore.token
    // console.log(token)
    //如果有token,直接跳转
    if (token) {
      // console.log('有token')
      next()
    } else {
      // console.log('没有token')
      //如果没有token就跳转登录页去获取token,并记录要去的页面
      next('/login' + `?redirect=${to.fullPath}`)
    }
  }




})
// 导出路由
export default router;